import Vue from 'vue'
import Vuex from 'vuex'
import { getUser } from '../service/getData'

Vue.use(Vuex)

const state = {
    latitude: '',   // 当前位置的纬度
    longitude: '',  // 当前位置的经度
    userInfo: null, // 用户信息
}

const mutations = {
    // 获取用户的信息
    ['get_userInfo'](state, info) {
        if(state.userInfo && (state.userInfo.username !== info.data.username)) {
            return
        }                                   
        if(!info.errno) {
            state.userInfo = {...info.data}
            state.userInfo.imgAvatar = 'https://gitee.com/uploads/68/999368_jzhyl.png?1480667564'
        }else {
            state.userInfo = null
        }
    }
}

const actions = {
    async getUserInfo({ commit, state }, payload) {
        let res = await getUser(payload.token)
        commit('get_userInfo', res)
    }
   
}

export default new Vuex.Store({
    state,
    mutations,
    actions
})